<template>
  <div class="goodsinfo">
    <!-- 顶部导航栏 -->
    <div class="mask" @click="maskshow" v-if="show"></div>
    <van-sticky :offset-top="0">
      <van-nav-bar
        title="商品详细"
        left-text="返回"
        left-arrow
        @click-left="onClickLeft"
      />
    </van-sticky>

    <!-- 轮播 -->
    <van-swipe class="my-swipe" :autoplay="3000" indicator-color="white">
      <van-swipe-item v-for="item in data.gallery" :key="item.id"
        ><img :src="item.img_url" alt=""
      /></van-swipe-item>
    </van-swipe>
    <!-- 30退换 -->
    <div class="serv">
      <div class="item"><span class="boll">o </span> 30天无忧退货</div>
      <div class="item"><span class="boll">o </span> 48小时快速退款</div>
      <div class="item"><span class="boll">o </span> 满88包邮</div>
    </div>
    <!-- 商品信息 -->
    <div class="info">
      <h2>{{ name }}</h2>
      <h6>{{ des }}</h6>
      <h5>¥ {{ price }}</h5>
    </div>

    <!-- 选择规格和数量 -->
    <div class="num" @click="isshow">
      <span class="left">选择规格和数量</span>
      <span class="right"><van-icon name="arrow" /></span>
    </div>

    <!-- 商品参数 -->
    <div class="goodspara">
      <h3>商品参数</h3>
      <div class="item" v-for="(item, index) in data.attribute" :key="index">
        <p class="left">{{ item.name }}</p>
        <p class="right">{{ item.value }}</p>
      </div>
    </div>

    <!-- 商品描述图片 -->
    <div class="img" v-html="html"></div>

    <!-- 常见问题 -->
    <div class="problem">
      <div class="title">
        <van-divider>常见问题</van-divider>
      </div>
      <div class="item" v-for="item in data.issue" :key="item.id">
        <h3>{{ item.question }}</h3>
        <h5>{{ item.answer }}</h5>
      </div>
    </div>
    <!-- 商品列表 -->
    <div class="goodslist">
      <van-divider class="look">大家都在看</van-divider>
      <div class="goods">
        <div
          @click="togoodsinfo(item.id)"
          class="item"
          v-for="item in data.productList"
          :key="item.id"
        >
          <img :src="item.list_pic_url" alt="" />
          <h3>{{ item.name }}</h3>
          <p>{{ item.retail_price }}</p>
        </div>
      </div>
    </div>

    <!-- 弹出选择规格 -->
    <div class="choice">
      <van-icon @click="maskshow" name="close" />
      <div class="goods">
        <img :src="pic" alt="" />
        <div class="con">
          <div class="price">¥{{ price }}</div>
          <div class="text">请选择数量</div>
        </div>
      </div>
      <div class="addnum">
        数量
        <span class="number" v-if="number">
          购物车内已存在此商品,数量为:{{ number }}</span
        >
      </div>
      <van-stepper v-model="value" />
    </div>
    <!-- 加入购物车 -->
    <div class="bottom">
      <div @click="collect" class="collect"><van-icon name="star" /></div>
      <div @click="tocar" class="car">
        <van-icon name="shopping-cart-o" :badge="num" />
      </div>
      <div class="buy" @click="buycar">立即购买</div>
      <div class="add" @click="buycar">加入购物车</div>
    </div>
  </div>
</template>

<script>
import {
  index,
  goodsList,
  detailaction,
  listaction,
  goodsdetailaction,
  addcollect,
} from "@/api/home/index";
import { listAction } from "@/api/my/index";

import { Toast } from "vant";
import { addCart, cartList, deleteAction } from "@/api/car/index";

import $ from "jquery";
export default {
  components: {},

  data() {
    return {
      goodsId: "",
      data: [],
      name: "",
      goodsid: "",
      des: "",
      price: "",
      html: "",
      value: 1,
      show: false,
      open: false,
      num: 0,
      pic: "",
      number: "",
    };
  },

  created() {
    localStorage.setItem("goodsID", this.$route.params.id);
    // 判断产品是不是已经在收藏的列表里面了
    listAction({
      openId: localStorage.getItem("openId"),
    }).then((res) => {
      console.log(res.collectGoodsList);
      var arr = res.collectGoodsList;
      console.log(this.$route.params.id);
      arr.forEach((ele) => {
        // console.log(ele);
        if (ele.id == this.$route.params.id) {
          console.log(ele);
          $(".collect").addClass("current");
        }
      });
    });

    // 判断商品在购物车内的数量
    cartList({
      openId: localStorage.getItem("openId"),
    }).then((res) => {
      console.log(res);
      res.data.forEach((el) => {
        if (el.goods_id == this.$route.params.id) {
          this.number = el.number;
        }
      });
    });

    // 加载购物车数量
    cartList({
      openId: localStorage.getItem("openId"),
    }).then((res) => {
      console.log(res);
      this.num = res.data.length;
    });

    // 获取商品详细
    this.goodsId = this.$route.params.id;
    if (this.$route.params.id) {
      localStorage.setItem("goodsId", this.$route.params.id);
    }

    goodsdetailaction({
      id: localStorage.getItem("goodsId"),
      openId: localStorage.getItem("openId"),
    }).then((res) => {
      console.log(res);
      this.pic = res.info.list_pic_url;
      this.name = res.info.name;
      this.des = res.info.goods_brief;
      this.price = res.info.retail_price;
      this.goodsid = res.info.id;
      if (res.info.goods_desc) {
        this.html = res.info.goods_desc;
      } else {
        this.html = "";
      }
      this.data = res;
    });
  },

  mounted() {},

  methods: {
    //点击下面的商品
    togoodsinfo(goodsid) {
      localStorage.setItem("goodsId", goodsid);
      this.$router.go(0);
    },
    // 加入购物车
    addcar() {
      addCart({
        goodsId: this.goodsid,
        number: this.value,
        openId: localStorage.getItem("openId"),
      }).then((res) => {
        console.log(res);
        if (res.data == "success") {
          Toast({
            message: "加入成功",
            icon: "like-o",
          });
        }
      });
    },
    // 前往购物车页面
    tocar() {
      this.$router.push({
        name: "car",
        params: {
          id: this.goodsid,
        },
      });
    },
    // 购买和加入购物车
    buycar() {
      if (this.open) {
        addCart({
          goodsId: this.goodsid,
          number: this.value,
          openId: localStorage.getItem("openId"),
        }).then((res) => {
          // console.log(res);
          if (res.data == "success") {
            Toast({
              message: "加入购物车成功",
              icon: "shopping-cart",
            });
          }
        });
        cartList({
          openId: localStorage.getItem("openId"),
        }).then((res) => {
          // console.log(res.data);
          this.num = res.data.length;
        });
        this.open = false;
        this.show = false;
        $(".choice").slideToggle("normal");
      } else {
        this.open = true;
        this.show = true;
        $(".choice").slideToggle("normal");
      }
    },
    // 收藏
    collect() {
      addcollect({
        goodsId: this.goodsid,
        openId: localStorage.getItem("openId"),
      }).then((res) => {
        // console.log(res);
      });
      $(".collect").toggleClass("current");
    },
    maskshow() {
      this.open = false;
      this.show = false;
      $(".choice").slideToggle("normal");
    },
    isshow() {
      this.open = true;
      this.show = true;
      $(".choice").slideToggle("normal");
    },
    onClickLeft() {
      this.$router.go(-1);
    },
  },

  computed: {},

  watch: {},
};
</script>

<style scoped lang='less'>
.van-swipe-item {
  img {
    width: 375px;
  }
}
.serv {
  width: 335px;
  height: 40px;
  line-height: 60px;
  display: flex;
  padding: 0 20px;
  justify-content: space-between;
  background-color: #f4f4f4;
  color: #666;
  .item {
    width: 100px;
    height: 40px;
    line-height: 40px;
    span {
      color: red;
    }
  }
}

// 商品信息价格
.info {
  background-color: #fff;
  padding: 20px 0;
  h2 {
    font-size: 24px;
    margin: 0;
  }
  h6 {
    font-size: 16px;
    color: #999;
    font-weight: 300;
    margin: 10px auto;
  }
  h5 {
    color: red;
    font-size: 26px;
    margin: 10px 0;
    margin-top: 20px;
    font-weight: 500;
  }
}

// 选择规格和数量
.num {
  width: 335px;
  padding: 0 20px;
  height: 40px;
  background-color: #fff;
  border-top: 1px solid #eee;
  overflow: hidden;
  display: flex;
  justify-content: space-between;
  margin-bottom: 10px;
  .left {
    height: 40px;
    line-height: 40px;
  }
  .right {
    font-size: 20px;
    height: 40px;
    line-height: 50px;
  }
}

// 商品参数
.goodspara {
  background-color: #fff;
  padding: 20px 0;
  h3 {
    width: 355px;
    font-size: 24px;
    text-align: left;
    margin: 20px auto;
  }
  .item {
    height: 40px;
    width: 355px;
    margin: 10px auto;
    background-color: rgb(241, 241, 241);
    display: flex;
    text-align: left;
    text-indent: 10px;
    border-radius: 4px;
    .left {
      height: 40px;
      line-height: 40px;
      width: 80px;
      color: #999;
    }
    .right {
      width: 275px;
      height: 40px;
      line-height: 40px;
    }
  }
}
// 常见问题
.problem {
  width: 375px;
  background-color: #fff;
  padding: 20px 0;
  margin-top: 20px;
  margin-bottom: 50px;
  .title {
    width: 335px;
    margin: 0 auto;
  }
  .item {
    text-align: left;
    margin: 0 auto;
    width: 335px;
    h3 {
      width: 335px;
    }
    h3::before {
      content: "* ";
      font-size: 20px;
      color: red;
    }
    h5 {
      color: #999;
      font-weight: 300;
      font-size: 13px;
      margin: 10px auto;
    }
  }
}

// 商品列表区
.goodslist {
  // background-color: #fff;
  overflow: hidden;
}
.look {
  width: 335px;
  height: 40px;
  padding: 0 20px;
  background-color: #fff;
  margin: 20px auto;
  margin-bottom: 0;
}
.goods {
  width: 375px;
  display: flex;
  justify-content: space-between;
  flex-wrap: wrap;
  .item {
    width: 185px;
    margin-bottom: 5px;
    background-color: #fff;
    img {
      width: 151px;
    }
    h3 {
      margin: 5px auto;
    }
    p {
      color: red;
      margin-bottom: 20px;
    }
  }
}
// 选择规则数量
.mask {
  position: fixed;
  left: 0;
  top: 0;
  z-index: 222;

  width: 375px;
  height: 667px;
  background-color: rgba(0, 0, 0, 0.4);
  transition: 0.4s;
}
.choice {
  display: none;
  z-index: 333;
  background-color: #fff;
  position: fixed;
  bottom: 50px;
  .van-icon {
    position: absolute;
    font-size: 30px;
    right: 10px;
    color: #999;
    top: 10px;
  }
  .goods {
    width: 335px;
    padding: 20px;
    display: flex;
    justify-content: space-between;
    img {
      float: left;
      width: 88px;
      height: 88px;
    }
    .con {
      width: 220px;
      text-align: left;
      .price {
        font-size: 18px;
        width: 220px;
        float: left;
        height: 30px;
        line-height: 30px;
        margin-top: 15px;
        color: red;
      }
      .text {
        width: 220px;
        float: left;
        height: 30px;
        line-height: 30px;
      }
    }
  }
  .addnum,
  .van-stepper {
    width: 335px;
    margin: 0 auto;
    text-align: left;
    .number {
      margin-left: 10px;
      color: red;
      opacity: 0.8;
    }
  }
  .van-stepper {
    margin: 20px;
  }
}
// 底部加入购物车
.bottom {
  z-index: 333;
  width: 375px;
  height: 50px;
  border-top: 1px solid #f4f4f4;
  background-color: #fff;
  display: flex;
  justify-content: space-between;
  overflow: hidden;
  position: fixed;
  bottom: 0;
  .current {
    color: red;
  }
  .collect,
  .car {
    width: 80px;
    font-size: 26px;
    border-right: 1px solid #f4f4f4;
    i {
      margin-top: 12px;
    }
  }
  .buy {
    width: 100px;
    line-height: 50px;
  }
  .add {
    width: 100px;
    line-height: 50px;
    color: #fff;
    background-color: red;
  }
}
</style>